package com.geese.module.business.controller;

import com.geese.common.BaseController;
import com.geese.common.entity.OperationLog;
import com.geese.common.entity.TransferRecordDO;
import com.geese.common.vo.Page;
import com.geese.exception.BusinessException;
import com.geese.module.business.service.ITransferRecordService;
import com.geese.module.business.vo.TransferRecordVO;
import com.geese.module.user.model.SecurityUser;
import com.geese.module.user.service.UserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * Created by huweiwei on 2017/3/27.
 */
@RestController
@RequestMapping("/business/transfer")
public class TransferRecordController extends BaseController{

    @Autowired
    private ITransferRecordService transferRecordService;
    @Autowired
    private UserService userService;

    /**
     * 商家打款记录
     * @param token
     * @param transferRecordVO
     * @return
     */
    @GetMapping("/list")
    public String list(@RequestParam String token, TransferRecordVO transferRecordVO) {
        if (transferRecordVO.getBusinessId() == null) {
            throw new BusinessException(120107);
        }
        Page page = getPageInfo(transferRecordVO);
        Map<String, Object> data = transferRecordService.selectList(page, transferRecordVO);
        return response(data);
    }

    /**
     * 商家确认打款
     * @param token
     * @param transferRecordDO
     * @return
     */
    @PostMapping("/pay")
    public String pay(@RequestParam String token, @RequestBody TransferRecordDO transferRecordDO) {
        if (StringUtils.isBlank(transferRecordDO.getPhotoUrl())) {
            throw new BusinessException(120103);
        }
        if (null == transferRecordDO.getId() || transferRecordDO.getId() <= 0) {
            throw new BusinessException(120009);
        }

        SecurityUser user = userService.getLoginUser(token);
        transferRecordService.doPay(user, transferRecordDO);
        return response();
    }

    /**
     * 商家首页获取金额相关信息
     * @param token
     * @return
     */
    @GetMapping("/index")
    public String index(@RequestParam String token) {
        SecurityUser user = userService.getLoginUser(token);
        Map<String, Object> data = transferRecordService.getIndex(user);
        return response(data);
    }
}
